<?php
include("./common/conn.php");
mysql_query('SET NAMES UTF8');
class Sub {
	/**
	 * [获取子账号]
	 * @Author   Axing
	 * @DateTime 2017-07-21
	 * @param    [int]     $shopid [厂商id]
	 * @return   [array]             [子账号数据]
	 */
	public function get_sub($shopid) {
		$sql = "SELECT id, sub_name, password FROM sub_shop WHERE shopid = " . $shopid;
		$result = mysql_query($sql);
		$i = 0;
		while($row = mysql_fetch_assoc($result)) {
			$data[$i]['id']			=		$row['id'];
			$data[$i]['sub_name']	=		$row['sub_name'];
			$data[$i]['password']	=		$row['password'];
			$i++;
		}
		return $data;
	}

	/**
	 * [新增子账号]
	 * @Author   Axing
	 * @DateTime 2017-07-21
	 * @param    [type]     $sub_name [账号名]
	 * @param    [type]     $shopid   [厂商id]
	 * @param    [type]     $password [密码]
	 */
	public function add($sub_name, $shopid, $password) {
		$sql = "SELECT id FROM sub_shop WHERE sub_name = " . "'" . $sub_name . "'";
		$result = mysql_fetch_assoc(mysql_query($sql));
		if($result == false) {
			$sql = "INSERT INTO sub_shop (sub_name, shopid, password) VALUES ('$sub_name', $shopid, $password)";
			$result = mysql_query($sql);
			if($result) {
				echo "<script>alert('新建账号成功');window.location.href='sub.php?action=get_sub'</script>";
			} else {
				echo "<script>alert('新建账号失败,请重新输入');window.location.href='sub.php?action=get_sub'</script>";
			}
		} else {
			echo "<script>alert('账号已存在,请重新输入');window.location.href='sub.php?action=get_sub'</script>";
		}

	}

	/**
	 * [获取单个子账号]
	 * @Author   Axing
	 * @DateTime 2017-07-21
	 * @param    [int]     $id   [子账号主键id]
	 * @return   [array]         [单个主账号数据]
	 */
	public function edit($id) {
		$sql = "SELECT id, sub_name, password FROM sub_shop WHERE id = " . $id;
		$edit = mysql_fetch_assoc(mysql_query($sql));
		return $edit;
	}

	/**
	 * [更改子账号]
	 * @Author   Axing
	 * @DateTime 2017-07-21
	 * @param    [int]     $_POST['id']   			 [子账号主键id]
	 * @param    [int]     $_POST['edit_sub_name']   [需要修改的账号名]
	 * @param    [int]     $_POST['edit_password']   [需要修改的密码]
	 * @return   [type]     [description]
	 */
	public function update() {
		if($_POST['edit_sub_name'] == null) {
			echo "<script>alert('账号不要留空');window.location.href='sub_edit.php?action=edit&id=" . $_POST['id'] . "'</script>";
		} elseif($_POST['edit_password'] == null) {
			echo "<script>alert('账号不要留空');window.location.href='sub_edit.php?action=edit&id=" . $_POST['id'] . "'</script>";
		}

		$sql = "UPDATE sub_shop SET sub_name = " . "'" . $_POST['edit_sub_name']. "'"  . ",password = " . $_POST['edit_password'] . " WHERE id = " . $_POST['id'];
		$result = mysql_query($sql);

		if($result) {
			echo "<script>alert('修改账号成功');window.location.href='sub.php?action=get_sub'</script>";
		} else {
			echo "<script>alert('新建账号失败,请重新输入');window.location.href='sub.php?action=get_sub'</script>";
		}
	}

	/**
	 * [删除子账号]
	 * @Author   Axing
	 * @DateTime 2017-07-21
	 * @param    [int]     $id  [子账号主键id]
	 * @return   [type]         [description]
	 */
	public function del($id) {
		$sql = "DELETE FROM sub_shop WHERE id = " . $id;
		$result = mysql_query($sql);
		if($result) {
			echo "<script>alert('删除账号成功');window.location.href='sub.php?action=get_sub'</script>";
		} else {
			echo "<script>alert('删除账号失败');window.location.href='sub.php?action=get_sub'</script>";
		}
	}
}

$sub = new Sub();

//$_GET['action'] 		= 	方法名
//$_POST['sub_name'] 	= 	账号
//$_POST['password']	=	密码

switch ($_GET['action']) {
	case 'get_sub':
		$res = $sub->get_sub($_SESSION['shopid']);
		break;
	
	case 'edit':
		$res = $sub->edit($_GET['id']);
		break;
	case 'del':
		$res = $sub->del($_GET['id']);
		break;
	case 'update':
		$res = $sub->update();
		break;
}

if($_POST['sub_name'] != null && $_POST['password'] != null) {
	$sub->add($_POST['sub_name'], $_SESSION['shopid'], $_POST['password']);
}